<template>
  <div>
    <Pane class="goods-add">
      <span slot="title">商品添加</span>
      <section slot="content">
        <el-form :model="goodsData" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品名称">
                <el-input v-model="goodsData.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-button type="primary" @click="saveGoods">添加商品</el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品分类">
                <el-select v-model="goodsData.category" placeholder="请选择商品类型">
                  <el-option label="满减专区" value="满减专区"></el-option>
                  <el-option label="爱情套餐" value="爱情套餐"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品价格">
                <el-input-number
                  v-model="goodsData.price"
                  @change="handleChange"
                  :min="1"
                  label="描述文字"
                ></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row> -->
          <!-- <el-col :span="12"> -->
          <el-form-item label="商品图片">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:5000/goods/goods_img_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              style="width:100px; height:100px"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" style="width:100px; height:100px" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <!-- </el-col> -->
          <!-- </el-row> -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品描述">
                <el-input type="textarea" :rows="4" v-model="goodsData.goodsDesc"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </section>
    </Pane>
  </div>
</template>

<script>
import Pane from '@/components/Pane/Pane.vue'
import { addGoods } from '@/api/goods.js'
export default {
  name: 'GoodsAdd',
  props: {},
  data() {
    return {
      goodsData: {
        name: '',
        // 分类
        category: '',
        price: '',
        imgUrl: '',
        goodsDesc: ''
      },
      imgBaseUrl: 'http://localhost:5000/upload/imgs/goods_img/',
      imageUrl: ''
    }
  },
  methods: {
    // 商品价格
    handleChange(value) {
      // console.log(this.goodsData.price)
    },
    // 头像上传
    handleAvatarSuccess(res, file) {
      const { code, msg, imgUrl } = res
      console.log(code, msg, imgUrl)
      if (code === 0) {
        this.$message.success(msg)
        this.imageUrl = this.imgBaseUrl + imgUrl
      }
      // this.imageUrl = URL.createObjectURL(file.raw)
      // console.log(this.imageUrl)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 添加商品
    async saveGoods() {
      // console.log(1)
      const imgUrlArr = this.imageUrl.split('/')
      const imgUrl = imgUrlArr[imgUrlArr.length - 1]
      const res = await addGoods({
        name: this.goodsData.name,
        category: this.goodsData.category,
        price: this.goodsData.price,
        imgUrl: imgUrl,
        goodsDesc: this.goodsData.goodsDesc
      })
      console.log(res)
    }
  },
  components: {
    Pane
  }
}
</script>

<style scoped >
.goods-add >>> .avatar-uploader {
  position: relative;
}
.goods-add >>> .avatar-uploader .el-icon-plus {
  position: relative;
  width: 100px;
  height: 100px;
  border: 1px dotted blue;
}
.goods-add >>> .avatar-uploader .el-icon-plus:before {
  content: '\e6d9';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
